package com.example.workflow.service.impl;

import com.example.workflow.bo.Expense;
import com.example.workflow.bo.ExpenseQueryBo;
import com.example.workflow.flow.BizFlowTypeEnum;
import com.example.workflow.mapper.ExpenseMapper;
import com.example.workflow.pojo.LoginUser;
import com.example.workflow.service.ExpenseService;
import com.example.workflow.service.UserInfoService;
import com.example.workflow.util.FlowUtil;
import com.example.workflow.vo.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ExpenseServiceImpl implements ExpenseService {

    @Autowired
    private ExpenseMapper expenseMapper;

    @Autowired
    private UserInfoService userInfoService;

    @Override
    @Transactional
    public void submit(Expense expense) {
        expenseMapper.insert(expense);
        List<UserInfoVo> userList = userInfoService.userList(null,null,null,null);

        log.info("userList " + userList);
        String type = expense.getType();
        userList = switch (type) {
            case "光缆故障", "新增光缆布放", "光缆定期巡检" -> userInfoService.userList(null,null, "光缆维护部", null);
            case "基站故障", "基站扩容", "基站设备保养", "网络架构调整" ->
                    userInfoService.userList(null,null, "基站运维部", null);
            case "电力供应保障", "电力设施升级", "电力设施维护", "重大活动电力保障" ->
                    userInfoService.userList(null,null, "电力保障部", null);
            case "服务器故障", "服务器采购", "服务器维护", "数据中心能效优化" ->
                    userInfoService.userList(null,null, "数据运维部", null);
            default -> userInfoService.userList(null,null, "应急抢修部", null);
        };
        Set<String> cityUsers = userList.stream().filter(x -> Objects.equals(x.getRole(), "CITY"))
                .map(item -> item.getId().toString()).collect(Collectors.toSet());
        Set<String> provinceUsers = userList.stream().filter(x -> Objects.equals(x.getRole(), "PROVINCE"))
                .map(item -> item.getId().toString()).collect(Collectors.toSet());
        Set<String> operatorUsers = userList.stream().filter(x -> Objects.equals(x.getRole(), "OPERATOR"))
                .map(item -> item.getId().toString()).collect(Collectors.toSet());
        Map<String,Object> params = new HashMap<>();
        params.put("cityUsers", cityUsers);
        params.put("provinceUsers", provinceUsers);
        params.put("operatorUsers", operatorUsers);
        params.put("duration","PT1M"); // 设置超时时间1为一分钟

        FlowUtil.startProcess(BizFlowTypeEnum.EXPENSE_APPROVE.getKey(),expense.getId(),params);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void approval(Expense expense, ExpenseQueryBo expenseQueryBo) {
//        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
//        Integer approvalId = loginUser.getUserInfo().getId();
        int approvalId = expenseQueryBo.getApprovalId();
        if(expenseQueryBo.getContent().equals("同意")) {
            // 通过
            Map<String,Object> params = new HashMap<>();
            params.put("pass",true);
            if(FlowUtil.claimAndCompleteTask(BizFlowTypeEnum.EXPENSE_APPROVE.getKey(),expense.getId().toString(),String.valueOf(approvalId),params)){
                expense.setStatus(expense.getStatus()+1);
            } else {
                expense.setStatus(expense.getStatus());
            }
        } else {
            //驳回
            Map<String,Object> params = new HashMap<>();
            params.put("pass",false);

            FlowUtil.destroyProcess(BizFlowTypeEnum.EXPENSE_APPROVE.getKey(),expense.getId().toString(),"不想通过",String.valueOf(approvalId));
            expense.setStatus(3);
        }
        expense.setApprovalId(1);
        expenseMapper.updateById(expense);
        log.info("update expense now!!!!!");
//        this.updateById(expense);
    }

}
